<template>
  <div>
    <el-upload
      ref="upload"
      :action="uploadUrl"
      :headers="headers"
      :on-success="handleAvatarSuccess"
    >
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">
        只能上传jpg/png文件，且不超过500kb
      </div>
    </el-upload>
    <div v-for="item in list.rows" :key="item.id" class="box-card">
      <el-card>
        <div slot="header" class="clearfix">
          <span>创建时间{{ item.createTime }}</span>
        </div>
        <div class="itemimg">
          <img :src="item.ossUrl" alt="" />
          <el-button type="danger" class="buttoned" @click="deletes(item.id)"
            >删除</el-button
          >
        </div>
      </el-card>
    </div>
    <div class="pagings">
      <div class="paging">
        <el-pagination
          :current-page="page.currentPage"
          :page-sizes="page.pageSizes"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import material from '@/api/material'
import mixin from '@/mixins'
export default {
  mixins: [mixin],
  data() {
    return {
      list: [],
    }
  },
  created() {
    this.findMaterialByPageApi()
  },
  mounted() {},
  methods: {
    findMaterialByPageApi() {
      material
        .findMaterialByPage(this.page.currentPage, this.page.pageSize, {})
        .then(res => {
          this.list = res.data
          if (res.success) {
            this.tableData = res.data.rows
            this.page.total = res.data.total
          } else {
            this.$message.error('请求失败')
          }
          console.log(this.list)
        })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page.pageSize = val
      this.findMaterialByPageApi()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page.currentPage = val
      this.findMaterialByPageApi()
    },
    //删除
    // deletes(row) {
    //   console.log(row, 'row')
    //   material.delMaterialApis(row).then(res => {
    //     console.log(res, 'resss')
    //     this.findMaterialByPageApi()
    //   })
    // },
    deletes(row) {
      console.log(row, '删除')
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        material.delMaterialApis(row).then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.findMaterialByPageApi()
          } else {
            this.$message.error(res.message)
          }
        })
      })
    },
    //上传文件
    handleAvatarSuccess(res, file) {
      console.log(res, 'resss')
      if (res.success) {
        this.ruleForm.coverImg = res.data.fileUrl
        this.$refs.upload.clearFiles()
      } else {
        this.$message.error('上传失败')
      }
    },
  },
}
</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.box-card {
  float: left;
  width: 18%;
  height: 400px;
  margin: 1%;
}
.itemimg img {
  width: 230px;
  height: 200px;
}
.itemimg {
  position: relative;
}
.paging {
  position: absolute;
  bottom: 30px;
}
.pagings {
  width: 404px;
  height: 32px;
}
.buttoned {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 70px;
  height: 5px;
  position: absolute;
  bottom: -22px;
  left: 160px;
}
</style>